package cn.xdwanj.ask.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xdwanj
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
        log.info("开始检查是否登录");
        var session = request.getSession();
        var username = ((String) session.getAttribute("username"));

        if (username == null) {
            throw new IllegalStateException("登录状态异常");
        }

        log.info("登录检查结束");
        return true;
    }

}
